﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Data.Models;

namespace Tests
{
    [TestFixture]
    public class PresentationModelsTests
    {
        [Test]
        public void ToDataTest()
        {
            DataModels dataModels = (new Data.Sources.Test()).GetData();
            Presentation.Models.PresentationModels presentationModels = new Presentation.Models.PresentationModels(dataModels);

            DataModels dataModels2 = presentationModels.ToData();

            foreach (Domain domain1 in dataModels.Domains)
            {
                bool aMatchingDomainExists = false;

                foreach (Domain domain2 in dataModels2.Domains)
                {
                    if (DomainsMatch(domain1,domain2))
                    {
                        aMatchingDomainExists = true;
                    }
                }
                Assert.IsTrue(aMatchingDomainExists);
            }

            foreach (Student student1 in dataModels.Students)
            {
                bool aMatchingStudentExists = false;

                foreach (Student student2 in dataModels2.Students)
                {
                    if (StudentsMatch(student1, student2))
                    {
                        aMatchingStudentExists = true;
                    }
                }

                Assert.IsTrue(aMatchingStudentExists);
            }

            foreach (Anecdote anecdote1 in dataModels.Anecdotes)
            {
                bool aMatchingAnecdoteExists = false;

                foreach (Anecdote anecdote2 in dataModels2.Anecdotes)
                {
                    if (AnecdotesMatch(anecdote1, anecdote2))
                    {
                        aMatchingAnecdoteExists = true;
                    }
                }
                Assert.IsTrue(aMatchingAnecdoteExists);
            }
        }

        private bool DomainsMatch(Domain domain1, Domain domain2)
        {
            return (domain1.ID == domain2.ID && domain1.Name == domain2.Name);
        }

        private bool StudentsMatch(Student student1, Student student2)
        {
            return (student1.ID == student2.ID && student1.Name == student2.Name);
        }

        private bool AnecdotesMatch(Anecdote anecdote1, Anecdote anecdote2)
        {
            return (  anecdote1.ID == anecdote2.ID
                   && anecdote1.DomainID == anecdote2.DomainID
                   && anecdote1.StudentID == anecdote2.StudentID
                   && anecdote1.Note == anecdote2.Note
                   && anecdote1.Date == anecdote2.Date);
        }
    }
}
